<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Illuminate\Support\Facades\DB;
use App\Helpers\SuccessResponse;
use App\Helpers\ValidatorResponse;
use App\Http\Constant;
use Ixudra\Curl\Facades\Curl;

class ForgetController extends Controller{

	public function __construct(){}

	public function sendCode(Request $request){
		$validator = Validator::make(['account' => $request->input('account')], [
			'account' => 'required|string',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$account = $request->input('account');
			$user = DB::connection('data_api_database')->select('select id from users where email = "'.$account.'" or phone_num = "'.$account.'"');
			$user = json_decode(json_encode($user), true);
			if(0 == count($user)){
				return SuccessResponse::handler('This account has not registered.');
			}else{
				$generate_OTP_url = env('DATABOSSJOB_URL', false);
				$generate_OTP_url .= '/api/v1/otps';
				$response = Curl::to($generate_OTP_url)
					->withData([
						'login' => $account,
					])
		        	->post();
		       	$response = json_decode($response, true);
		       	if(isset($response['data']['id'])){
		       		$otp_id = $response['data']['id'];
		       		$otp = DB::connection('data_api_database')->select('select * from otps where id = '.$otp_id);
		       		$otp = json_decode(json_encode($otp), true);
		       		$code = $otp[0]['otp'];
		       		$rtn = [
						'data_response' => $response,
						'md5_code' => $this->_getMD5CodeFromCode($account, $code),
						'code' => $code.' (For dev)',
					];
		       	}else{
		       		$rtn = [
						'data_response' => $response,
						'message' => 'Something error on Data Bossjob',
					];
		       	}
			}
					
			return SuccessResponse::handler($rtn);
		}
	}

	public function checkCode(Request $request){
		$validator = Validator::make($request->all(), [
			'account' => 'required|string',
			'code' => 'required|string:4',
			'md5_code' => 'required|string:32',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$md5Code = $request->input('md5_code');
			$code = $request->input('code');
			$account = $request->input('account');
			if($md5Code == $this->_getMD5CodeFromCode($account, $code)){
				return SuccessResponse::handler(['commit_id' => $this->_getMD5CodeFromCode($account, $md5Code)]);
			}else{
				return SuccessResponse::handler('invalidate verify code');
			}
		}
	}

	public function checkCommitId(Request $request){
		$validator = Validator::make($request->all(), [
			'account' => 'required|string',
			'code' => 'required|string',
			'md5_code' => 'required|string',
			'commit_id' => 'required|string',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$md5Code = $request->input('md5_code');
			$code = $request->input('code');
			$account = $request->input('account');
			if($md5Code == $this->_getMD5CodeFromCode($account, $code)){
				$commitId = $request->input('commit_id');
				if($commitId == $this->_getMD5CodeFromCode($account, $md5Code)){
					return SuccessResponse::handler(['commit_id' => $commitId, 'checked' => 1, 'message' => 'validate commit id']);
				}else{
					return SuccessResponse::handler(['commit_id' => $commitId, 'checked' => 0, 'message' => 'invalidate commit id']);
				}
			}else{
				return SuccessResponse::handler('invalidate verify code');
			}
		}
	}

	public function forgetAndResetPwd(Request $request){
		$validator = Validator::make($request->all(), [
			'account' => 'required|string',
			'code' => 'required|string',
			'md5_code' => 'required|string',
			'commit_id' => 'required|string',
			'password' => 'required|string|min:6|max:18|confirmed',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$md5Code = $request->input('md5_code');
			$code = $request->input('code');
			$account = $request->input('account');
			if($md5Code == $this->_getMD5CodeFromCode($account, $code)){
				$commitId = $request->input('commit_id');
				if($commitId == $this->_getMD5CodeFromCode($account, $md5Code)){
					$pwd = $request->input('password');
					$reset_pwd_url = env('DATABOSSJOB_URL', false);
					$reset_pwd_url .= '/api/v1/users/reset_password';
					$response = Curl::to($reset_pwd_url)
						->withData([
							'login' => $account,
							'new_password' => $pwd,
							'otp' => $code,
						])
			        	->patch();
			       	$response = json_decode($response, true);
			       	return SuccessResponse::handler($response);
				}else{
					return SuccessResponse::handler(['commit_id' => $commitId, 'checked' => 0, 'message' => 'invalidate commit id']);
				}
			}else{
				return SuccessResponse::handler('invalidate verify code');
			}
		}
	}

	private function _getMD5CodeFromCode($account, $code){
		return md5($account.sha1($code).'bossjob'.$code);
	}
}
